<template>
  <div>
    <el-button type="info" icon="el-icon-caret-left" @click="$router.go(-1)" size="small">返回</el-button>
    <el-form ref="Attendance" :model="AttendanceInfo" :rules="Check">
      <el-row>
        <el-col :span="10">
          <el-form-item label="类型" prop="TypeID"><br>
            <el-select v-model="AttendanceInfo.TypeID" placeholder="选择类型" style="width:100%">
              <el-option v-for="(item,index) in options" :key="index" :label="item.TypeName_CHS" :value="item.ID">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="StatusID"><br>
            <el-select v-model="AttendanceInfo.StatusID" placeholder="选择类型" style="width:100%">
              <el-option v-for="(item,index) in options2" :key="index" :label="item.StatusName_CHS" :value="item.ID">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4"><br></el-col>
        <el-col :span="10">
          <el-form-item label="备注" prop="Description">
            <el-input v-model="AttendanceInfo.Description" placeholder="请输入你的备注"></el-input>
          </el-form-item>
          <el-form-item label="审核" prop="Note">
            <el-input v-model="AttendanceInfo.Note" placeholder="请输入你的审核"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <hr>
      <el-button type="primary" @click="submit('Attendance')" size="small">保存</el-button>
      <el-button type="success" @click="$router.go(-1)" size="small">取消</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      options: [],
      options2: [],
      AttendanceInfo: {
        TypeID: null,
        Description: null
      },
      FullName: '',
      Type: 'Add',
      Check: {
        TypeID: [
          { required: true, message: '请输入你的类型', trigger: 'blur' }
        ],
        Description: [
          { required: true, message: '请输入你的备注', trigger: 'blur' }
        ],
      }
    }
  },
  mounted () {
    this.$axios({
      url: '/HrApi/Attendance/GetTypeList',
      method: 'GET',
    }).then(res => {
      if (!res.data.code) {
        this.options = res.data.data;
        this.options2 = res.data.data2;
      }
    })
    //编辑
    if (Object.keys(this.$route.query).length) {
      this.Type = "Edit";
      this.$axios({
        url: '/HrApi/Attendance/GetAttendanceFullName',
        method: 'GET',
        params: {
          pk_Attendance: this.$route.query["pk_Attendance"]
        }
      }).then(res => {
        if (!res.data.code) {
          this.AttendanceInfo = res.data.data.AttendanceInfo
          this.FullName = res.data.data.FullName
          let dt = this.AttendanceInfo.CreateDate.split('T').join(' ')
          if (dt.split('.').length > 1) {
            dt = dt.split('.')[0]
          }
          this.$route.meta.title = `考勤审核：${this.FullName}(${dt})`
        } else {
          this.$message.error(res.data.msg)
        }
      })
    }
  },
  methods: {
    submit (Attendance) {
      //表单验证
      this.$refs[Attendance].validate((valid) => {
        if (!valid) {
          return false;
        }
        //执行请求
        this.$axios({
          url: "/HrApi/Attendance/UpdateAttendanceList",
          method: "POST",
          data: this.$Qs.stringify({
            AttendanceInfo: JSON.stringify(this.AttendanceInfo),
            Type: this.Type,
            pk_User: this.$store.state.UserInfo.pk_User
          })
        }).then(res => {
          if (!res.data.code) {
            this.ListDt = res.data.data;
            this.$message.success(res.data.msg)
            this.$refs['Attendance'].resetFields();
          } else {
            this.$message.error(res.data.msg);
          }
        })

      })
    }
  },
  filters: {
    TimeFilter (t) {
      if (t === null) {
        return '无'
      }
      let dt = t.split('T').join(' ')
      if (dt.split('.').length > 1) {
        dt = dt.split('.')[0]
      }
      return dt
    }
  }
}
</script>